package com.macro.mall.controller;

import com.alibaba.excel.EasyExcel;
import com.macro.mall.component.PmsItemExcelListener;
import com.macro.mall.dto.PmsItem;
import com.macro.mall.dto.PmsItemExcel;
import com.macro.mall.service.PmsItemService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author：Minxiao-Hao
 * @Description: 物品Excel导入导出控制层
 * @name：PmsItemExcelController
 * @Date：2024/11/3 10:01
 */
@RestController
@RequestMapping("item/excel")
public class PmsItemExcelController {
    @Autowired
    private PmsItemService pmsItemService;

    @Autowired
    private PmsItemExcelListener pimItemExcelListener;
    @Operation(summary = "导出物品Excel")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ResponseBody
    public void export(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=pms_item_export.xlsx");
        // 获取数据
        List<PmsItem> list = pmsItemService.list();
        List<PmsItemExcel> pmsItemExcellist = list.stream().map(item -> {
            PmsItemExcel pmsItemExcel = new PmsItemExcel();
            BeanUtils.copyProperties(item, pmsItemExcel);
            return pmsItemExcel;
        }).toList();

        try {
            EasyExcel.write(response.getOutputStream(), PmsItemExcel.class)
                    .sheet("物品数据")
                    .doWrite(pmsItemExcellist);
        } catch (IOException e) {
            throw new RuntimeException("导出失败", e);
        }
    }

    @Operation(summary = "选择导出物品Excel")
    @RequestMapping(value = "/selectExport/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public void selectExport(HttpServletResponse response, @PathVariable("ids") List<Long> ids) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=pms_item_export.xlsx");
        // 获取数据
        List<PmsItemExcel> list = pmsItemService.selectList(ids);
        try {
            EasyExcel.write(response.getOutputStream(), PmsItemExcel.class)
                    .sheet("物品数据")
                    .doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException("导出失败", e);
        }
    }
    @Operation(summary = "导入物品Excel")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ResponseBody
    public void importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("上传的文件为空");
        }

        try {
                EasyExcel.read(file.getInputStream(), PmsItemExcel.class,pimItemExcelListener).sheet().doRead();
        } catch (Exception e) {
            throw new RuntimeException("导入失败", e);
        }
    }
}
